home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cocktail / doc.lha / doc.doc / reuseC.doc < prev    next >
Text File  |  1992-09-25  |  28KB  |  1,068 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11. ___________________________________________________________________
  12.  
  13.  
  14.                                    Reusable Software
  15.                                    A Collection of C-Modules
  16.  
  17.                                    J. Grosch
  18.  
  19.  
  20. ___________________________________________________________________
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50. ___________________________________________________________________
  51.                                    GESELLSCHAFT FUeR MATHEMATIK
  52.                                    UND DATENVERARBEITUNG MBH
  53.  
  54.                                    FORSCHUNGSSTELLE FUeR
  55.                                    PROGRAMMSTRUKTUREN
  56.                                    AN DER UNIVERSITAeT KARLSRUHE
  57. ___________________________________________________________________
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.                                    Project
  76.  
  77.                              Compiler Generation
  78.  
  79.          ___________________________________________________________
  80.  
  81.                               Reusable Software
  82.                           A Collection of C-Modules
  83.  
  84.  
  85.                                  Josef Grosch
  86.  
  87.  
  88.                                  Aug. 4 1992
  89.  
  90.          ___________________________________________________________
  91.  
  92.  
  93.                                 Report No. 30
  94.  
  95.  
  96.                              Copyright c 1992 GMD
  97.  
  98.  
  99.             Gesellschaft fuer Mathematik und Datenverarbeitung mbH
  100.                 Forschungsstelle an der Universitaet Karlsruhe
  101.                           Vincenz-Priesznitz-Str. 1
  102.                                D-7500 Karlsruhe
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                                   Reuse - C                                  2
  135.  
  136.  
  137. Abstract
  138.  
  139. A brief description of my personal collection of reusable modules written in C
  140. is  given.   The  modules  are oriented towards compiler construction.  Origi-
  141. nally, the modules have been written in MODULA-2.
  142.  
  143. 1.  Overview
  144.  
  145.      The following modules exist currently (Aug. 1992):
  146.  
  147.        ________________________________________________________________
  148.         Module      Task
  149.        ________________________________________________________________
  150.         Memory      dynamic storage (heap) with free lists
  151.         DynArray    dynamic and flexible arrays
  152.         StringMem   string memory
  153.         Idents      identifier table - unambiguous encoding of strings
  154.         Sets        sets of scalar values (without run time checks)
  155.         Positions   handling of source positions
  156.         Errors      error handler for parsers and compilers
  157.         Source      provides input for scanners
  158.         General     miscellaneous functions
  159.         System      machine dependent code
  160.         Time        access to cpu-time
  161.        ________________________________________________________________
  162.       
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.                                                                       
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189. 2.  Memory: dynamic storage (heap) with free lists
  190.  
  191.  
  192. extern unsigned long MemoryUsed;
  193.                         /* Holds the total amount of memory managed by  */
  194.                         /* this module.                                 */
  195.  
  196. extern void     InitMemory      ();
  197.                         /* The memory module is initialized.            */
  198.  
  199. extern char *   Alloc           (register unsigned long ByteCount);
  200.                         /* Returns a pointer to dynamically allocated   */
  201.                         /* space of size 'ByteCount' bytes.             */
  202.  
  203. extern void     Free            (unsigned long ByteCount, char * a);
  204.                         /* The dynamically allocated space starting at  */
  205.                         /* address 'a' of size 'ByteCount' bytes is     */
  206.                         /* released.                                    */
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.                                   Reuse - C                                  3
  226.  
  227.  
  228. 3.  DynArray: dynamic and flexible arrays
  229.  
  230.      This module provides dynamic and flexible arrays. The size of  a  dynamic
  231. array  is  determined  at run time. It must be passed to a procedure to create
  232. the array. The size of such an array is also flexible, that means it can  grow
  233. to arbitrary size by repeatedly calling a procedure to extend it.
  234.  
  235.  
  236. extern void MakeArray    (char * * ArrayPtr, unsigned long * ElmtCount,
  237.                           unsigned long ElmtSize);
  238.                         /* 'ArrayPtr' is set to the start address of a  */
  239.                         /* memory space to hold an array of 'ElmtCount' */
  240.                         /* elements each of size 'ElmtSize' bytes.      */
  241.  
  242. extern void ExtendArray  (char * * ArrayPtr, unsigned long * ElmtCount,
  243.                           unsigned long ElmtSize);
  244.                         /* The memory space for the array is increased  */
  245.                         /* by doubling the number of elements.          */
  246.  
  247. extern void ReleaseArray (char * * ArrayPtr, unsigned long * ElmtCount,
  248.                           unsigned long ElmtSize);
  249.                         /* The memory space for the array is released.  */
  250.  
  251.  
  252. Example:
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.                                   Reuse - C                                  4
  291.  
  292.  
  293. # define         InitialSize    100
  294. typedef ...      ElmtType;
  295. typdef  ElmtType ArrayType [100000];
  296. unsigned long    ActualSize = InitialSize;
  297. ElmtType *       ArrayPtr;
  298.  
  299. MakeArray (& ArrayPtr, & ActualSize, sizeof (ElmtType);
  300.  
  301. (* Case 1: continuously growing array *)
  302.  
  303. Index := 0;
  304. for (;;) {
  305.    Index ++;
  306.    if (Index == ActualSize)
  307.       ExtendArray (& ArrayPtr, & ActualSize, sizeof (ElmtType);
  308.  
  309.    ArrayPtr [Index] = ... ;   /* access an array element */
  310.     ... = ArrayPtr [Index];   /* "      "  "     "       */
  311. }
  312.  
  313. (* Case 2: non-continuously growing array *)
  314.  
  315. for (;;) {
  316.    Index = ... ;
  317.    while (Index >= ActualSize)
  318.       ExtendArray (& ArrayPtr, & ActualSize, size of(ElmtType);
  319.  
  320.    ArrayPtr [Index] = ... ;   /* access an array element */
  321.     ... = ArrayPtr [Index];   /* "      "  "     "       */
  322. }
  323.  
  324. ReleaseArray (& ArrayPtr, & ActualSize, sizeof (ElmtType);
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.                                   Reuse - C                                  5
  356.  
  357.  
  358. 4.  StringMem: string memory
  359.  
  360.  
  361. typedef unsigned short * tStringRef;
  362.  
  363. extern  tStringRef PutString    (register char * s, register cardinal length);
  364.                         /* Stores string 's' in the string memory and   */
  365.                         /* returns a reference to the stored string.    */
  366.  
  367. extern  void    StGetString     (register tStringRef r, register char * s);
  368.                         /* Returns the string 's' from the string       */
  369.                         /* memory which is referenced by 'r'.           */
  370.  
  371. /* extern cardinal LengthSt     (register tStringRef r); */
  372. # define LengthSt(stringref) (* stringref)
  373.                         /* Returns the length of the string 's'         */
  374.                         /* which is referenced by 'r'.                  */
  375.  
  376. extern  bool    IsEqualSt       (tStringRef r, register char * s);
  377.                         /* Compares the string referenced by 'r' and    */
  378.                         /* the string 's'.                              */
  379.                         /* Returns true if both are equal.              */
  380.  
  381. extern  void    WriteString     (FILE * f, tStringRef r);
  382.                         /* The string referenced by 'r' is printed on   */
  383.                         /* the file 'f'.                                */
  384.  
  385. extern  void    WriteStringMemory ();
  386.                         /* The contents of the string memory is printed */
  387.                         /* on standard output.                          */
  388.  
  389. extern  void    InitStringMemory ();
  390.                         /* The string memory is initialized.            */
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.                                   Reuse - C                                  6
  421.  
  422.  
  423. 5.  Idents: identifier table - unambiguous encoding of strings
  424.  
  425.  
  426. typedef cardinal        tIdent;
  427.  
  428. extern  tIdent  NoIdent; /* A default identifer (empty string)          */
  429.  
  430. extern  tIdent  MakeIdent       (char * string, cardinal length);
  431.                         /* The string (of length) is mapped to a unique */
  432.                         /* identifier (an integer) which is returned.   */
  433.  
  434. extern  void    GetString       (tIdent ident, char * string);
  435.                         /* Returns the string whose identifier is 'ident'.*/
  436.  
  437. extern  tStringRef GetStringRef (tIdent ident);
  438.                         /* Returns a reference to the string identified */
  439.                         /* by 'ident'.                                  */
  440.  
  441. extern  tIdent  MaxIdent        ();
  442.                         /* Returns the currently maximal identifier.    */
  443.  
  444. extern  void    WriteIdent      (FILE * file, tIdent ident);
  445.                         /* The string encoded by the identifier 'ident' */
  446.                         /* is printed on the file.                      */
  447.  
  448. extern  void    WriteIdents     ();
  449.                         /* The contents of the identifier table is      */
  450.                         /* printed on the standard output.              */
  451.  
  452. extern  void    InitIdents      ();
  453.                         /* The identifier table is initialized.         */
  454.  
  455. extern  void    WriteHashTable  ();
  456.  
  457.  
  458. 6.  Sets: sets for scalar values
  459.  
  460.      The following module provides operations on sets of  scalar  values.  The
  461. elements  of  the  sets can be of the types int, unsigned, char, or long.  The
  462. size of the sets, that is the range the elements must  lie  in,  is  not  res-
  463. tricted. The elements can range from 0 to 'MaxSize', where space for arbitrary
  464. large sets.
  465.  
  466.      The sets are implemented as bit vectors (long [])  plus  some  additional
  467. information  to  improve  performance.  So  don't  worry  about speed too much
  468. because procedures like Select, Extract, or Card  are  quite  efficient.  They
  469. don't  execute a loop over all potentially existing elements always. This hap-
  470. pens only in the worst case.
  471.  
  472.  
  473.  
  474.  
  475.  
  476.  
  477.  
  478.  
  479.  
  480.  
  481.  
  482.  
  483.  
  484.  
  485.                                   Reuse - C                                  7
  486.  
  487.  
  488. # define BitsPerBitset          32
  489. # define LdBitsPerBitset        5
  490. # define MaskBitsPerBitset      0x0000001f
  491.  
  492. # define IsElement(Elmt, Set) ((int) ((Set)->BitsetPtr [(Elmt) >> LdBitsPerBitset] \
  493.                                     << ((Elmt) & MaskBitsPerBitset)) < 0)
  494. # define Size(Set)                  ((Set)->MaxElmt)
  495. # define Select(Set)                Minimum (Set)
  496. # define IsNotEqual(Set1, Set2)     (! IsEqual (Set1, Set2))
  497. # define IsStrictSubset(Set1, Set2) (IsSubset (Set1, Set2) && \
  498.                                      IsNotEqual (Set1, Set2))
  499.  
  500. typedef long    BITSET          ;
  501.  
  502. typedef struct  {
  503.       cardinal  MaxElmt         ;
  504.       cardinal  LastBitset      ;
  505.       BITSET *  BitsetPtr       ;
  506.       short     Card            ;
  507.       cardinal  FirstElmt       ;
  508.       cardinal  LastElmt        ;
  509.    } tSet;
  510.  
  511. extern void     MakeSet         (tSet * Set, cardinal MaxSize);
  512. extern void     ReleaseSet      (tSet * Set);
  513. extern void     Union           (tSet * Set1, tSet * Set2);
  514. extern void     Difference      (tSet * Set1, tSet * Set2);
  515. extern void     Intersection    (tSet * Set1, tSet * Set2);
  516. extern void     SymDiff         (tSet * Set1, tSet * Set2);
  517. extern void     Complement      (tSet * Set);
  518. extern void     Include         (tSet * Set, cardinal Elmt);
  519. extern void     Exclude         (tSet * Set, cardinal Elmt);
  520. extern cardinal Card            (tSet * Set);
  521. /* extern cardinal      Size            (tSet * Set); */
  522. extern cardinal Minimum         (tSet * Set);
  523. extern cardinal Maximum         (tSet * Set);
  524. /* extern cardinal      Select          (tSet * Set); */
  525. extern cardinal Extract         (tSet * Set);
  526. extern bool     IsSubset        (tSet * Set1, tSet * Set2);
  527. /* extern bool  IsStrictSubset  (tSet * Set1, tSet * Set2); */
  528. extern bool     IsEqual         (tSet * Set1, tSet * Set2);
  529. /* extern bool  IsNotEqual      (tSet * Set1, tSet * Set2); */
  530. /* extern bool  IsElement       (cardinal Elmt, tSet * Set); */
  531. extern bool     IsEmpty         (tSet * Set);
  532. extern bool     Forall          (tSet * Set, bool (* Proc) ());
  533. extern bool     Exists          (tSet * Set, bool (* Proc) ());
  534. extern bool     Exists1         (tSet * Set, bool (* Proc) ());
  535. extern void     Assign          (tSet * Set1, tSet * Set2);
  536. extern void     AssignElmt      (tSet * Set, cardinal Elmt);
  537. extern void     AssignEmpty     (tSet * Set);
  538. extern void     ForallDo        (tSet * Set, void (* Proc) ());
  539. extern void     ReadSet         (FILE * File, tSet * Set);
  540. extern void     WriteSet        (FILE * File, tSet * Set);
  541.  
  542.  
  543.  
  544.  
  545.  
  546.  
  547.  
  548.  
  549.  
  550.  
  551.                                   Reuse - C                                  8
  552.  
  553.  
  554. extern void     InitSets        ();
  555.  
  556.  
  557.      Two parameters of type 'tSet' passed to one of the above procedures  must
  558. have  the  same  size, that is they must have been created by passing the same
  559. value 'MaxSize' to the procedure 'MakeSet'. A parameter representing  an  ele-
  560. ment  (of  type  CARDINAL or equivalent) passed to one of the above procedures
  561. must have a value between 0 and 'MaxSize' of the involved  set  which  is  the
  562. other  parameter passed. If the two conditions above, which can be verified at
  563. programming time, don't hold then strange things will  happen,  because  there
  564. are no checks at run time, of course.
  565.  
  566.  
  567. The following table explains the semantics of the set operations:
  568.  
  569. Procedure        Semantics
  570. ___________________________________________________________________________________________
  571. MakeSet          allocates space for a set to hold elements
  572.                  ranging from 0 to 'MaxSize'.
  573. ReleaseSet       releases the space taken by a set.
  574. Union            Set1 := Set1 U Set2
  575. Difference       Set1 := Set1 - Set2
  576. Intersection     Set1 := Set1  Set2
  577. SymDiff          Set1 := Set1  Set2     (* corresponds to exclusive or *)
  578. Complement       Set := { 0 .. MaxSize } - Set
  579. Include          Set := Set U { Elmt }
  580. Exclude          Set := Set - { Elmt }
  581. Card             returns number of elements in Set
  582. Size             returns 'MaxSize' given at creation time
  583. Minimum          returns smallest element x from Set
  584. Maximum          returns largest element x from Set
  585. Select           returns arbitrary element x from Set
  586. Extract          returns arbitrary element x from Set and removes it from Set
  587. IsSubset         Set1  Set2
  588. IsStrictSubset   Set1  Set2
  589. IsEqual          Set1 = Set2
  590. IsNotEqual       Set1 / Set2
  591. IsElement        Elmt  Set
  592. IsEmpty          Set =
  593. Forall            e  Set : Proc (e)   /* predicate Proc must hold for all elements */
  594. Exists            e  Set : Proc (e)   /* predicate Proc must hold for at least 1 element */
  595. Exists1          | { e  Set : Proc (e) } | = 1
  596. Assign           Set1 := Set2
  597. AssignElmt       Set1 := { Elmt }
  598. AssignEmpty      Set1 :=
  599. ForallDo         FOR e := 0 TO MaxSize DO
  600.                       IF e  Set THEN Proc (e); END;
  601.                  END;
  602. ReadSet          read external representation of a set from file 'tFile'.
  603. WriteSet         write external representation of a set to file 'tFile'.
  604.                  Example output: { 0 5 6 123}
  605.  
  606.  
  607.  
  608.  
  609.  
  610.  
  611.  
  612.  
  613.  
  614.  
  615.  
  616.                                   Reuse - C                                  9
  617.  
  618.  
  619. 7.  Positions: handling of source positions
  620.  
  621.      A simple representation of the position of tokens in a source  file  con-
  622. sisting  of  a  line  and  a  column  field.  This module should be copied and
  623. tailored to the user's needs, if necessary. Modifications may be necessary  if
  624. the  type SHORTCARD is to small to count the lines or an extra field is needed
  625. to describe the source file.
  626.  
  627.  
  628. typedef struct { unsigned short Line, Column; } tPosition;
  629.  
  630. extern tPosition NoPosition;
  631.                         /* A default position (0, 0).                   */
  632.  
  633. extern int  Compare       (tPosition Position1, tPosition Position2);
  634.                         /* Returns -1 if Position1 < Position2.         */
  635.                         /* Returns  0 if Position1 = Position2.         */
  636.                         /* Returns  1 if Position1 > Position2.         */
  637.  
  638. extern void WritePosition (FILE * File, tPosition Position);
  639.                         /* The 'Position' is printed on the 'File'.     */
  640.  
  641.  
  642. 8.  Errors: error handler for parsers and compilers
  643.  
  644.      This module is needed by parsers generated  with  the  parser  generators
  645. lalr  or  ell.  It can also b used to report error messages found during scan-
  646. ning or semantic analysis.  Note: This module has to be copied,  too,  if  the
  647. module Positions is copied and modified because it depends upon this module.
  648.  
  649.  
  650. # define xxNoText               0
  651. # define xxSyntaxError          1       /* error codes          */
  652. # define xxExpectedTokens       2
  653. # define xxRestartPoint         3
  654. # define xxTokenInserted        4
  655. # define xxTooManyErrors        5
  656.  
  657. # define xxFatal                1       /* error classes        */
  658. # define xxRestriction          2
  659. # define xxError                3
  660. # define xxWarning              4
  661. # define xxRepair               5
  662. # define xxNote                 6
  663. # define xxInformation          7
  664.  
  665. # define xxNone                 0
  666. # define xxInteger              1       /* info classes         */
  667. # define xxShort                2
  668. # define xxLong                 3
  669. # define xxReal                 4
  670. # define xxBoolean              5
  671. # define xxCharacter            6
  672.  
  673.  
  674.  
  675.  
  676.  
  677.  
  678.  
  679.  
  680.  
  681.  
  682.                                   Reuse - C                                 10
  683.  
  684.  
  685. # define xxString               7
  686. # define xxSet                  8
  687. # define xxIdent                9
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699.  
  700.  
  701.  
  702.  
  703.  
  704.  
  705.  
  706.  
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719.  
  720.  
  721.  
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.  
  731.  
  732.  
  733.  
  734.  
  735.  
  736.  
  737.  
  738.  
  739.  
  740.  
  741.  
  742.  
  743.  
  744.  
  745.  
  746.  
  747.                                   Reuse - C                                 11
  748.  
  749.  
  750. extern void (* Errors_Exit) ();
  751.                         /* Refers to a procedure that specifies         */
  752.                         /* what to do if 'ErrorClass' = Fatal.          */
  753.                         /* Default: terminate program execution.        */
  754.  
  755. extern void StoreMessages (bool Store);
  756.                         /* Messages are stored if 'Store' = TRUE        */
  757.                         /* for printing with the routine 'WriteMessages'*/
  758.                         /* otherwise they are printed immediately.      */
  759.                         /* If 'Store'=TRUE the message store is cleared.*/
  760.  
  761. extern void ErrorMessage  (int ErrorCode, int ErrorClass, tPosition Position);
  762.                         /* Report a message represented by an integer   */
  763.                         /* 'ErrorCode' and classified by 'ErrorClass'.  */
  764.  
  765. extern void ErrorMessageI (int ErrorCode, int ErrorClass, tPosition Position,
  766.                            int InfoClass, char * Info);
  767.                         /* Like the previous routine with additional    */
  768.                         /* information of type 'InfoClass' at the       */
  769.                         /* address 'Info'.                              */
  770.  
  771. extern void Message       (char * ErrorText, int ErrorClass, tPosition Position);
  772.                         /* Report a message represented by a string     */
  773.                         /* 'ErrorText' and classified by 'ErrorClass'.  */
  774.  
  775. extern void MessageI      (char * ErrorText, int ErrorClass, tPosition Position,
  776.                            int InfoClass, char * Info);
  777.                         /* Like the previous routine with additional    */
  778.                         /* information of type 'InfoClass' at the       */
  779.                         /* address 'Info'.                              */
  780.  
  781. extern void WriteMessages (FILE * File);
  782.                         /* The stored messages are sorted by their      */
  783.                         /* source position and printed on 'File'.       */
  784.  
  785.  
  786.  
  787.  
  788.  
  789.  
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.  
  797.  
  798.  
  799.  
  800.  
  801.  
  802.  
  803.  
  804.  
  805.  
  806.  
  807.  
  808.  
  809.  
  810.  
  811.  
  812.                                   Reuse - C                                 12
  813.  
  814.  
  815. 9.  Source: provides input for scanners
  816.  
  817.      This module is needed by scanners generated with  the  scanner  generator
  818. rex.
  819.  
  820.  
  821. extern int  BeginSource  (char * FileName);
  822.  
  823.    /*
  824.       BeginSource is called from the scanner to open files.
  825.       If not called input is read form standard input.
  826.    */
  827.  
  828. extern int  GetLine      (int File, char * Buffer, int Size);
  829.  
  830.    /*
  831.       GetLine is called to fill a buffer starting at address 'Buffer'
  832.       with a block of maximal 'Size' characters. Lines are terminated
  833.       by newline characters (ASCII = 0xa). GetLine returns the number
  834.       of characters transferred. Reasonable block sizes are between 128
  835.       and 2048 or the length of a line. Smaller block sizes -
  836.       especially block size 1 - will drastically slow down the scanner.
  837.    */
  838.  
  839. extern void CloseSource  (int File);
  840.  
  841.    /*
  842.       CloseSource is called from the scanner at end of file respectively
  843.       at end of input. It can be used to close files.
  844.    */
  845.  
  846.  
  847. 10.  General: miscellaneous functions
  848.  
  849.  
  850. # define Min(a,b) ((a <= b) ? a : b)
  851.                         /* Returns the minimum of 'a' and 'b'.          */
  852. # define Max(a,b) ((a >= b) ? a : b)
  853.                         /* Returns the maximum of 'a' and 'b'.          */
  854.  
  855. extern cardinal         Log2 (register unsigned long x);
  856.                         /* Returns the logarithm to the base 2 of 'x'.  */
  857. extern unsigned long    Exp2 (register cardinal x);
  858.                         /* Returns 2 to the power of 'x'.               */
  859.  
  860.  
  861.  
  862.  
  863.  
  864.  
  865.  
  866.  
  867.  
  868.  
  869.  
  870.  
  871.  
  872.  
  873.  
  874.  
  875.  
  876.  
  877.                                   Reuse - C                                 13
  878.  
  879.  
  880. 11.  System: machine dependent code
  881.  
  882.      This module provides a few machine dependent operations.
  883.  
  884.  
  885. /* interface for machine dependencies */
  886.  
  887. # define tFile int
  888.  
  889. /* binary IO */
  890.  
  891. extern tFile    OpenInput       (char * FileName);
  892.                         /* Opens the file whose name is given by the    */
  893.                         /* string parameter 'FileName' for input.       */
  894.                         /* Returns an integer file descriptor.          */
  895.  
  896. extern tFile    OpenOutput      (char * FileName);
  897.                         /* Opens the file whose name is given by the    */
  898.                         /* string parameter 'FileName' for output.      */
  899.                         /* Returns an integer file descriptor.          */
  900.  
  901. extern int      Read            (tFile File, char * Buffer, int Size);
  902.                         /* Reads 'Size' bytes from file 'tFile' and     */
  903.                         /* stores them in a buffer starting at address  */
  904.                         /* 'Buffer'.                                    */
  905.                         /* Returns the number of bytes actually read.   */
  906.  
  907. extern int      Write           (tFile File, char * Buffer, int Size);
  908.                         /* Writes 'Size' bytes from a buffer starting   */
  909.                         /* at address 'Buffer' to file 'tFile'.         */
  910.                         /* Returns the number of bytes actually written.*/
  911.  
  912. extern void     Close           (tFile File);
  913.                         /* Closes file 'tFile'.                         */
  914.  
  915. extern bool IsCharacterSpecial  (tFile File);
  916.                         /* Returns TRUE when file 'tFile' is connected  */
  917.                         /* to a character device like a terminal.       */
  918.  
  919.  
  920. /* calls other than IO */
  921.  
  922. extern char *   SysAlloc        (long ByteCount);
  923.                         /* Returns a pointer to dynamically allocated   */
  924.                         /* memory space of size 'ByteCount' bytes.      */
  925.                         /* Returns NIL if space is exhausted.           */
  926.  
  927. extern long     Time            ();
  928.                         /* Returns consumed cpu-time in milliseconds.   */
  929.  
  930. extern int      GetArgCount     ();
  931.                         /* Returns number of arguments.                 */
  932.  
  933.  
  934.  
  935.  
  936.  
  937.  
  938.  
  939.  
  940.  
  941.  
  942.                                   Reuse - C                                 14
  943.  
  944.  
  945. extern void     GetArgument     (int ArgNum, char * Argument);
  946.                         /* Stores a string-valued argument whose index  */
  947.                         /* is 'ArgNum' in the memory area 'Argument'.   */
  948.  
  949. extern void     PutArgs         (int Argc, char * * Argv);
  950.                         /* Dummy procedure that passes the values       */
  951.                         /* 'argc' and 'argv' from Modula-2 to C.        */
  952.  
  953. extern int      ErrNum          ();
  954.                         /* Returns the current system error code.       */
  955.  
  956. extern int      System          (char * String);
  957.                         /* Executes an operating system command given   */
  958.                         /* as the string 'String'. Returns an exit or   */
  959.                         /* return code.                                 */
  960.  
  961. extern void     Exit            (int Status);
  962.                         /* Terminates program execution and passes the  */
  963.                         /* value 'Status' to the operating system.      */
  964.  
  965. extern void     BEGIN_System    ();
  966.                         /* Dummy procedure with empty body.             */
  967.  
  968.  
  969. 12.  Time: access to cpu-time
  970.  
  971.  
  972. extern int      StepTime        ();
  973.                         /* Returns the sum of user time and system time */
  974.                         /* since the last call to 'StepTime' in milli-  */
  975.                         /* seconds.                                     */
  976.  
  977. extern void     WriteStepTime   (char * string);
  978.                         /* Writes a line consisting of the string       */
  979.                         /* 'string' and the value obtained from a call  */
  980.                         /* to 'StepTime' on standard output.            */
  981.  
  982.  
  983.  
  984.  
  985.  
  986.  
  987.  
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.  
  995.  
  996.  
  997.  
  998.  
  999.  
  1000.  
  1001.  
  1002.  
  1003.  
  1004.  
  1005.  
  1006.  
  1007.                                   Reuse - C                                  1
  1008.  
  1009.  
  1010. Contents
  1011.  
  1012.         Abstract ........................................................    2
  1013. 1.      Overview ........................................................    2
  1014. 2.      Memory: dynamic storage (heap) with free lists ..................    2
  1015. 3.      DynArray: dynamic and flexible arrays ...........................    3
  1016. 4.      StringMem: string memory ........................................    5
  1017. 5.      Idents: identifier table - unambiguous encoding of strings ......    6
  1018. 6.      Sets: sets for scalar values ....................................    6
  1019. 7.      Positions: handling of source positions .........................    9
  1020. 8.      Errors: error handler for parsers and compilers .................    9
  1021. 9.      Source: provides input for scanners .............................   12
  1022. 10.     General: miscellaneous functions ................................   12
  1023. 11.     System: machine dependent code ..................................   13
  1024. 12.     Time: access to cpu-time ........................................   14
  1025.  
  1026.  
  1027.  
  1028.  
  1029.  
  1030.  
  1031.  
  1032.  
  1033.  
  1034.  
  1035.  
  1036.  
  1037.  
  1038.  
  1039.  
  1040.  
  1041.  
  1042.  
  1043.  
  1044.  
  1045.  
  1046.  
  1047.  
  1048.  
  1049.  
  1050.  
  1051.  
  1052.  
  1053.  
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060.  
  1061.  
  1062.  
  1063.  
  1064.  
  1065.  
  1066.  
  1067.  
  1068.